//
//  ByUIMinePointsVC.m
//  chequanbao_ios
//
//  Created by mc on 2017/5/16.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIMinePointsVC.h"

// view
#import "ByViewMinePointHeader.h"

// cell
#import "ByCellMineConsume.h"

// api
#import "ByApiMine.h"

@interface ByUIMinePointsVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ByViewMinePointHeader *headerView;

// data
@property (nonatomic, strong) NSMutableArray *showAry;
@property (nonatomic, copy)   NSString *change_type;
@property (nonatomic, assign) NSInteger page_index;
@end

@implementation ByUIMinePointsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的收入";
    [self addLeftBackBarButtonItem];
    [self initTableView];
    
    [self.tableView.mj_header beginRefreshing];
    
    self.change_type = @"2";
}

- (void)viewSafeAreaInsetsDidChange{
    [super viewSafeAreaInsetsDidChange];
    _headerView.top = self.view.safeAreaInsets.top;
    _tableView.top = _headerView.bottom;
    _tableView.height = self.view.height - self.view.safeAreaInsets.bottom - self.view.safeAreaInsets.top - 80 - 30;
}

#pragma mark -- getter
-(ByViewMinePointHeader *)headerView{
    if (!_headerView) {
        _headerView = [[ByViewMinePointHeader alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 80 + 30 )];
    }
    return _headerView;
}

-(NSMutableArray *)showAry{
    if (!_showAry) {
        _showAry = [NSMutableArray array];
    }
    return _showAry;
}
#pragma amrk -- 获取数据
// 上拉加载更多
-(void)loadMoreData{
    ByCoreRefreshGifFooter *footer = [ByCoreRefreshGifFooter footerWithRefreshingBlock:^{
        self.page_index += 1;
        [self fetchData];
    }];
    self.tableView.mj_footer = footer;
}

// 下拉刷新
- (void)upLoadData{
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        self.page_index = 1;
        [self fetchData];
    }];
    self.tableView.mj_header = header;
}

// 获取数据
- (void)fetchData{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.uid = BY_USER.uid;
    model.role_type = BY_USER.account_id;
    model.change_type = self.change_type;
    model.page_index = [NSString stringWithFormat:@"%ld",self.page_index];
    model.page_size = @"10";
    [ByCoreToolAlert showLoadind:@"加载中..."];
    [ByApiMine changeRecord:model success:^(ByNetBaseResult *result) {
        NSInteger count = [result.data[@"count"] integerValue];
        NSMutableArray *temp = [NSMutableArray array];
        for (ByModelMinePoints *model in result.data[@"list"]) {
            [temp addObject:model];
        }
        if (self.page_index == 1) {
            [self.showAry removeAllObjects];
            [self.showAry addObjectsFromArray:[temp copy]];
            [self.tableView.mj_footer resetNoMoreData];
        }else{
            [self.showAry addObjectsFromArray:[temp copy]];
        }
        [self.tableView reloadData];
        [ByCoreToolAlert hideHud];
        if (self.showAry.count == count) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return ;
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];

}

#pragma mark -- 创建视图
- (void)initTableView{
    [self.view addSubview:self.headerView];
    __weak typeof(self) weakSelf = self;
    self.headerView.selectBlock = ^(NSInteger index){
        weakSelf.change_type = index == 0?@"2":@"1";
        weakSelf.page_index = 1;
        [weakSelf fetchData];
    };
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.headerView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 80 - 30) style:UITableViewStyleGrouped];
    _tableView.estimatedRowHeight = 0;
    _tableView.estimatedSectionHeaderHeight = 0;
    _tableView.estimatedSectionFooterHeight = 0;
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self upLoadData];
    [self loadMoreData];
    self.page_index = 1;

}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.showAry.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByCellMineConsume *cell = [ByCellMineConsume cellWithTableView:tableView];
    cell.points_model = self.showAry[indexPath.section];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}
@end
